const jwt = require('jsonwebtoken');
const userModel = require('../schema/user');
const db_user = {
    account: 'zhang',
    nickname: 'zhang',
    password: '123456',
    age: 18,
    sex: '男',
    phone: '12345678901',
    email: '12345678901@qq.com',
    isAdmin: true,
};
module.exports = {
    login: async (req, res, next) => {
        try {
            // const db_user = req.body; // 确保从请求体中获取用户数据
            let token = jwt.sign({...db_user}, req.config.secretKey, {expiresIn: 60 * 60 * 24 * 7});
            const modelRecord = new userModel(
                {...db_user}
            );
            await modelRecord.save()

            return res.send({success: true, user: db_user, token, message: 'login success'});
        } catch (e) {
            return res.send({success: false, message: e.message});
        }
    },
    logout: (req, res, next) => {
        try {
            return res.send({success: true, message: 'logout success'});

        } catch (e) {
            return res.send({success: false, message: e.message});
        }
    }
}